package com.cfets.annualAssessment.backgroundController;

import com.cfets.annualAssessment.entity.SMSBlack;
import com.cfets.annualAssessment.entity.TableContainer;
import com.cfets.annualAssessment.entity.annualInformMore;
import com.cfets.annualAssessment.service.SMSBlackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by ShenYiChou on 2017/2/3.
 */
@Controller
@RequestMapping("/admin/blacklist")
public class SMSBlackController {
    @Autowired
    private SMSBlackService smsBlackService;
    @RequestMapping("/index")
    public String index(Model model){
        return "blacklist";
    }
    @RequestMapping(value = "/Get",method = RequestMethod.POST)
    public ResponseEntity<?> smsBlackList(SMSBlack smsBlack){
        List<SMSBlack> smsBlackList = smsBlackService.getSMSBlackPage(smsBlack);
        TableContainer tableContainer = new TableContainer();
        tableContainer.setAaData(smsBlackList);
        tableContainer.setiTotalDisplayRecords(smsBlack.getTotalCount());
        tableContainer.setiTotalRecords(smsBlack.getTotalCount());
        return new ResponseEntity<>(tableContainer, HttpStatus.OK);
    }

    @RequestMapping(value = "/smsBlackMore",method = RequestMethod.POST)
    public ResponseEntity<?> smsBlackMoreProcess (annualInformMore annualInformMore) {
        String[] item;
        boolean flag = true;
        if (annualInformMore.getIds() != null) {
            item = annualInformMore.getIds().split(",");
            //批量删除
            if (annualInformMore.getProcess() == 1) {
                for (int i = 0; i < item.length; i++) {
                    flag = this.smsBlackService.deleteSMSBlackById(Integer.parseInt(item[i]));
                    if (flag == false) {
                        return new ResponseEntity<>(flag, HttpStatus.OK);
                    }
                }
            }
        }
        return new ResponseEntity<>(flag, HttpStatus.OK);
    }

    @PostMapping
    public ResponseEntity<?> newSMSBlack(@Valid SMSBlack smsBlack, BindingResult result) {
        if (result.hasErrors()) {
            return new ResponseEntity<>(result.getAllErrors(), HttpStatus.OK);
        }
        boolean flag = false;
        if(smsBlack.getId()==null){
            flag = this.smsBlackService.newSMSBlack(smsBlack);
        }
        return new ResponseEntity<>(flag, HttpStatus.OK);
    }
}
